﻿using Abp;
using Abp.Application.Services;
using FwProject.Manager;
using FwProject.ShopApplication;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Abp.ObjectMapping;
using FwProject.ShoppingMall.EventAdvertising;
using Abp.Domain.Repositories;
using X.PagedList;
using System.Linq;
using AutoMapper.QueryableExtensions;
using FwProject.ShoppingMall.Com;
using FwProject.Com.Dto;
using Abp.AutoMapper;

namespace FwProject.Com.Dto
{
    /// <summary>
    /// 广告位管理
    /// </summary>
    public class ShopSupplierAppService : FwProjectAppServiceBase, IShopSupplierAppService
    {
        private readonly ShopSupplierManager _shopSupplierManager;
        private readonly IRepository<ShopSupplier, Guid> _shopAdLocationRepository;
        private Abp.IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="shopSupplierManager"></param>
        /// <param name="shopAdLocationRepository"></param>
        public ShopSupplierAppService(ShopSupplierManager shopSupplierManager, IRepository<ShopSupplier, Guid> shopAdLocationRepository)
        {

            _shopSupplierManager = shopSupplierManager;
            _guid = SequentialGuidGenerator.Instance;
            _map = NullObjectMapper.Instance;
            _shopAdLocationRepository = shopAdLocationRepository;
        }
        /// <summary>
        /// 创建代理商
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<Guid> CreateShopSupplier(ShopSupplierInput Input)
        {
            var model = Input.MapTo<ShopSupplier>();
            model.Id = _guid.Create();
            return await _shopSupplierManager.CreateShopSupplier(model);
        }
        /// <summary>
        /// 修改一个代理商
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task EditShopAdLocation(Guid Id, ShopSupplierInput Input)
        {
            var model = _shopAdLocationRepository.Get(Id);
            Input.MapTo(model);
            await _shopSupplierManager.UpdateShopSupplier(model);
        }
        /// <summary>
        /// 删除一个代理商
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task DeleteAdLocation(Guid Id)
        {
            var model = _shopAdLocationRepository.Get(Id);
            await _shopSupplierManager.DeleteShopSupplier(model);
        }
        /// <summary>
        /// 获取一个供货商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ShopSupplierDto> GetByIdAsync(Guid id)
        {
            var model = await _shopAdLocationRepository.GetAsync(id);
            return _map.Map<ShopSupplierDto>(model);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="SupplierName"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="IsActive"></param>
        /// <returns></returns>
        public async Task<IPagedList<ShopSupplierDto>> GetPageList(int page, int pageSize, string SupplierName, DateTime? beginTime, DateTime? endTime, bool? IsActive)
        {

            var list = _shopAdLocationRepository.GetAll();
            if (!string.IsNullOrEmpty(SupplierName))
                list = list.Where(o => o.SupplierName.Contains(SupplierName));

            if (beginTime != null)
                list = list.Where(o => o.CreationTime >= beginTime);

            if (endTime != null)
                list = list.Where(o => o.CreationTime <= endTime.Value.AddHours(24));

            if (IsActive != null)
                list = list.Where(o => o.IsActive == IsActive);

            return await list.OrderByDescending(o => o.CreationTime).ProjectTo<ShopSupplierDto>().ToPagedListAsync(page, pageSize);
        }
        /// <summary>
        /// 获取一个模型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ShopSupplierInput> GetEnterDtoAsync(Guid id)
        {
            var entity = await _shopAdLocationRepository.GetAsync(id);
            var dto = entity.MapTo<ShopSupplierInput>();
            return dto;
        }
        /// <summary>
        /// 获取供应商模型
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public async Task<LayerDataBaseDto> GetSupplierLayerDataAsync(int page, int limit, string keyword)
        {
            var query = _shopAdLocationRepository.GetAll();

            if (!string.IsNullOrWhiteSpace(keyword))
                query = query.Where(o => o.SupplierName.Contains(keyword));

            var pagelist = await query.OrderByDescending(o => o.CreationTime).ProjectTo<ShopSupplierDto>().ToPagedListAsync(page, limit);

            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = pagelist;
            dto.Count = pagelist.TotalItemCount;
            return dto;
        }
        /// <summary>
        /// 设置供应商激活状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="active"></param>
        /// <returns></returns>
        public async Task SwitchActive(Guid id, bool active)
        {
            var entity = await _shopAdLocationRepository.GetAsync(id);
            entity.IsActive = active;
            await _shopAdLocationRepository.UpdateAsync(entity);
        }
        /// <summary>
        /// 获取所有的供应商
        /// </summary>
        /// <returns></returns>
        public async Task<List<ShopSupplierDto>> GetListAsync()
        {
            var query = _shopAdLocationRepository.GetAll().Where(o => o.IsActive);

            var list = await query.OrderByDescending(o => o.CreationTime).ProjectTo<ShopSupplierDto>().ToListAsync();

            return list;
        }
    }
}
